<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-25
 * Time: 11:55
 */


function get_top_menu($rule, $type = 0,$level = 1) {
    $menu = \think\Db::name("MerMenu")->where("rule_value",$rule)->find();
    if($menu['level'] <= $level) {
        if($type == 0) {
            return $rule;
        }else {
            return $menu;
        }
    }else {
        $m2 = \think\Db::name("merMenu")->where("id", $menu['pid'])->find();
        return get_top_menu($m2['rule_value'],$type, $level);
    }
}

function archive_option($table, $key, $val, $selected,$mer_id){
    $archive = \think\Db::name($table)->where("mer_id",$mer_id)->select();
    $html = "";
    foreach($archive as $_) {
        $html .= "<option value='{$_[$val]}' ".($_[$val] == $selected?"selected":"").">{$_[$key]}</option>";
    }
    return $html;
}

function archive_checkbox($table, $key, $val, $selected,$mer_id,$name) {
    $sel_val = explode(",",$selected);
    $archive = \think\Db::name($table)->where("mer_id",$mer_id)->select();
    $html = "";
    foreach ($archive as $i => $_){
        $html .= "<input type='checkbox' name='{$name}[{$i}]' title='{$_[$key]}' value = '{$_[$val]}' ".(in_array($_[$val],$sel_val)?"checked":"").">";
    }
    return $html;
}

function area_option($pid, $selected) {
    $areas = \think\Db::name("Area")->where("pid", $pid)->select();
    $html = "";
    foreach ($areas as $_) {
        $html .= "<option value='{$_['id']}' ".($_['id'] == $selected?"selected":"").">{$_['name']}</option>";
    }
    return $html;
}